<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="设备Mac" prop="faultMac">
        <el-input
          v-model="queryParams.faultMac"
          placeholder="请输入设备Mac"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建人" prop="creatorName">
        <el-input
          v-model="queryParams.creatorName"
          placeholder="请输入创建人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="维修人员" prop="technicianName">
        <el-input
          v-model="queryParams.technicianName"
          placeholder="请输入维修人员"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="维修工作开始时间" prop="startTime">
        <el-date-picker clearable
                        v-model="queryParams.startTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择维修工作开始时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="维修工作结束时间" prop="endTime">
        <el-date-picker clearable
                        v-model="queryParams.endTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择维修工作结束时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="工单状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择工单状态" clearable>
          <el-option
            v-for="dict in dict.type.maintenance_progress"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:workorders:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:workorders:edit']"
        >维修反馈
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:workorders:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:workorders:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="workordersList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <!--      <el-table-column label="工单唯一标识符" align="center" prop="id" />-->
      <!--      <el-table-column label="关联的故障唯一标识符，外键到Faults表" align="center" prop="faultId" />-->
      <el-table-column label="设备Mac" align="center" prop="faultMac"/>
      <!--      <el-table-column label="工单创建人ID" align="center" prop="creatorId" />-->
      <el-table-column label="创建人" align="center" prop="creatorName"/>
      <!--      <el-table-column label="维修人员ID" align="center" prop="technicianId" />-->
      <el-table-column label="维修人员" align="center" prop="technicianName"/>
      <el-table-column label="维修工作开始时间" align="center" prop="startTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="维修工作结束时间" align="center" prop="endTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="工单状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.maintenance_progress" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="维修反馈" align="center" prop="notes"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <!--          <el-button-->
          <!--            size="mini"-->
          <!--            type="text"-->
          <!--            icon="el-icon-edit"-->
          <!--            @click="handleUpdate(scope.row)"-->
          <!--            v-hasPermi="['system:workorders:edit']"-->
          <!--          >修改-->
          <!--          </el-button>-->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:workorders:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改工单记录，用于跟踪故障的维修工作进度对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="故障信息" prop="faultId">
          <!--          <el-input v-model="form.faultId" placeholder="请输入关联的故障唯一标识符，外键到Faults表"/>-->
          <el-select v-model="form.faultId" placeholder="请选择故障信息">
            <el-option
              v-for="dict in faultslist"
              :key="dict.faultId"
              :label="dict.deviceId"
              :value="dict.faultId"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="维修人员" prop="technicianId">
          <el-select v-model="form.technicianId" placeholder="请选择维修人员">
            <el-option
              v-for="dict in serviceUsers"
              :key="dict.userId"
              :label="dict.userName"
              :value="dict.userId"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog :title="title" :visible.sync="msgOpen" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="维修反馈" prop="notes">
          <el-input v-model="form.notes" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import {listWorkorders, getWorkorders, delWorkorders, addWorkorders, updateWorkorders} from "@/api/system/workorders";
import {listFaults} from "@/api/system/faults";
import {allocatedUserList} from "@/api/system/role";
// import faultslist from "@/views/system/common/faultslist";
// import Faultslist from "@/views/system/common/faultslist.vue";

export default {
  name: "Workorders",
  components: {},
  dicts: ['maintenance_progress'],
  data() {
    return {
      dialogTitle: "",
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 工单记录，用于跟踪故障的维修工作进度表格数据
      workordersList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      msgOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        faultId: null,
        faultMac: null,
        creatorId: null,
        creatorName: null,
        technicianId: null,
        technicianName: null,
        startTime: null,
        endTime: null,
        status: null,
        notes: null
      },
      // 表单参数
      form: {
        faultId: null,
        technicianId:null
      },
      // 表单校验
      rules: {},
      // 故障列表
      faultslist: [],
      //维修人员列表
      serviceUsers: []
    };
  },

  created() {
    this.getList();
  },
  methods: {
    /** 查询工单记录，用于跟踪故障的维修工作进度列表 */
    getList() {
      this.loading = true;
      listWorkorders(this.queryParams).then(response => {
        this.workordersList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.msgOpen = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        faultId: null,
        faultMac: null,
        creatorId: null,
        creatorName: null,
        technicianId: null,
        technicianName: null,
        startTime: null,
        endTime: null,
        status: null,
        notes: null
      };
      this.resetForm("form");
      this.faultslist = []
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      listFaults({
        pageNum: 1,
        pageSize: 10000,
        status: 1
      }).then(res => {
        console.log("获取故障信息.....", res)

        this.faultslist = res.rows
      })
      allocatedUserList({
        pageNum: 1,
        pageSize: 10000,
        roleId: 102
      }).then(res => {
        this.serviceUsers = res.rows
      })
      this.open = true;
      this.title = "添加工单记录，用于跟踪故障的维修工作进度";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getWorkorders(id).then(response => {
        this.form = response.data;
        this.msgOpen = true;
        this.title = "修改工单记录，用于跟踪故障的维修工作进度";
      });
    },
    getInfo(item) {
      console.log(item);
      this.addWorkorders({orderId: item.id, id: this.ruleForm.id}).then(res => {
        if (res.code == 200) {

        } else {
          this.$message.error(res.msg);
        }
      });
    },
    /** 提交按钮 */
    submitForm() {

      if (this.form.id != null) {
        updateWorkorders(this.form).then(response => {
          this.$modal.msgSuccess("修改成功");
          this.open = false;
          this.getList();
        });
      } else {
        console.log("aaaaaaa")
        console.log(this.form)
        addWorkorders(this.form).then(response => {
          this.$modal.msgSuccess("新增成功");
          this.open = false;
          this.getList();
        });
      }
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除工单记录，用于跟踪故障的维修工作进度编号为"' + ids + '"的数据项？').then(function () {
        return delWorkorders(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/workorders/export', {
        ...this.queryParams
      }, `workorders_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
